#!/bin/sh
# Verify that wisotool verbs run to completion
# Optional arg: which group of verbs to run.  Default is 'all'.

set -x

case "$OS" in
 "Windows_NT")
    # Mostly unimplemented...
    # Cheezy fix for getting rid of double slashes when running cygwin in wine
    case "$HOME" in
      /) HOME="" ;;
    esac
    WINE=""
    WINESERVER=true
    DRIVE_C="C:/"
    ;;
 *)
    export WINE=${WINE:-$HOME/wine-git/wine}
    export WINESERVER=${WINESERVER:-$HOME/wine-git/server/wineserver}
    ;;
esac

srcdir=`dirname $0`
srcdir=`cd $srcdir; pwd`

# verbs known to not work yet
BLACKLIST="starcraft$|passmark7|smbx"

case "$LANG" in
""|"C") echo "Some games won't install in the Posix locale; please do 'export LANG=en_US.UTF-8' or something like that" ; exit 1;;
esac

case "$1" in
download)  verbs="`sh wisotool --help | grep '\[download' | grep -v broken | egrep -v "$BLACKLIST" | awk '{print $1}'`" ;;
loaded|all|"") verbs="`sh wisotool ls | egrep -v "$BLACKLIST" `" ;;
activision) verbs="
        blur
        codmw2
        " ;;
ea) verbs="
        bfbc2
        cnc3_demo                
        deadspace
        dragonage
        fifa10_demo
        fifaonline3_beta         
        hphbp
        hphbp_demo
        nfsps_demo               
        nfsshift
        nfsshift_demo
        sims3
        spore 
        masseffect2
        " ;;
        # masseffect install hangs, so do it last

positech) verbs="democracy2_demo gsb_demo kudos2_demo rocklegend_demo" ;;

ubisoft) verbs="
        assassinscreed
        avatar_demo  
        popsot
        popsot_demo
        " ;;
*) echo "Guessing you specified a single verb"; verbs=$1;;
esac

for app in $verbs
do
    export WINEPREFIX=$HOME/wisotooltest-prefixes/$app
    # change if you don't want to skip ones already installed
    if test -d $HOME/wisotooltest-prefixes/$app
    then
        echo "Skipping $app, already installed"
        continue
        #rm -rf $WINEPREFIX
    fi
    export DRIVE_C="$WINEPREFIX/dosdevices/c:"

    # Isolate us from the user's home directory
    sh winetricks sandbox gecko

    echo "Installing $app"
    if ! sh wisotool $app
    then
        echo "Installing $app failed!"
        #exit 1
    fi
    echo -n "Done installing $app, disk usage "
    du -sh $WINEPREFIX
    $WINESERVER -w
    echo "Wineserver done."
    ps augxw | grep exe
    # Verify the installation
    if [ -f "$srcdir/wisotoolverify.d/$app.sha1sum" ]
    then
        windir="`$WINE cmd /c echo "%windir%" | cut -c 4- | tr -d '\015'`"
        progdir="`$WINE cmd /c echo "%ProgramFiles%" | cut -c 4- | tr -d '\015'`"

        cd "$DRIVE_C"
        # Fix up the filenames, which can differ between Windows versions/Wine:
        # FIXME: we need a way to guarantee that all _original_ .sha1sums are the same.
        # Preferably generated under 32-bit wine, so that we don't need a really complex sed
        # substitution here...
        sed -e "s|/Program\ Files/|/$progdir/|" -e "s|/windows/|/$windir/|" < "$srcdir/wisotoolverify.d/$app.sha1sum" > $app.sha1sum.tmp
        sha1sum -c $app.sha1sum.tmp || ( echo "sha1sum check failed!" ; exit 1 )
        rm $app.sha1sum.tmp
        cd $srcdir
    fi
done
